package com.wuzihao.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuzihao.reggie.common.R;
import com.wuzihao.reggie.dto.SetmealDto;
import com.wuzihao.reggie.entity.Setmeal;
import com.wuzihao.reggie.service.SetmealDishService;
import com.wuzihao.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        Page pageInfo = setmealService.pageInfo(page,pageSize,name);

        return R.success(pageInfo);
    }

    /**&
     * List提供批量删除功能
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);

        return R.success("套餐删除成功");
    }

    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        setmealService.updateById(setmealDto);

        return R.success("套餐修改成功");
    }

    //获取移动端主界面点击套餐展示页面
    // TODO 不用@RequestBody
    @GetMapping("/list")
    public R list(Setmeal setmeal){
        List<Setmeal> list = setmealService.listSetmeal(setmeal);

        return R.success(list);
    }
}
